﻿/*
BF4Rcon.NET, provides a .NET implementation of BF4's RCON interface.
Copyright (C) 2013 Tim Marro

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using TimScripts.BF4Rcon.Frostbite.Resources;

namespace TimScripts.BF4Rcon.Frostbite
{
    public class Packet : IPacket
    {
        public const int MaxSequenceValue = (1 << 30) - 1;
        public bool IsFromClient { get; private set; }

        public bool IsResponse { get; private set; }

        private int _Sequence;
        public int Sequence
        {
            get
            {
                return _Sequence;
            }

            private set
            {
                if (value < 0 || value > MaxSequenceValue) throw new ArgumentOutOfRangeException("value",
                    string.Format(System.Globalization.CultureInfo.InvariantCulture,
                    PortableErrorStrings.Argument_SequenceValue,
                    MaxSequenceValue));

                _Sequence = value;
            }
        }

        private List<string> _WordsList;
        private ReadOnlyCollection<string> _Words;
        public ReadOnlyCollection<string> Words
        {
            get { return _Words; }
        }

        public Packet(bool isFromClient, bool isResponse, int sequence, IEnumerable<string> words)
        {
            if (words == null || !words.Any()) throw new ArgumentException(PortableErrorStrings.Argument_NoWords, "words");

            IsFromClient = isFromClient;
            IsResponse = isResponse;
            Sequence = sequence;

            _WordsList = new List<string>(words);
            _Words = new ReadOnlyCollection<string>(_WordsList);
        }

        public Packet(bool isFromClient, bool isResponse, int sequence, params string[] words)
            : this(isFromClient, isResponse, sequence, (IEnumerable<string>)words)
        {

        }

        IList<string> IPacket.Data
        {
            get { return Words; }
        }
    }
}
